#!/bin/sh

## $Id: export-tarball,v 1.1 2005/11/23 18:09:55 korpela Exp $

# Create a tarball from CVS or SVN export.

# example usage:

# CVSROOT=/usr/local/warez/cvsroot                                             \
#     CHECKOUT='cvs export -r HEAD boinc'                                      \
#     DIR='boinc'                                                              \
#     FILENAME_TGZ="boinc-cvs-TODAY.tar.gz"                                    \
#     FILENAME_ZIP="boinc-cvs-TODAY.zip"                                       \
#     DESTINATION="/disks/milkyway/a/users/anderson/boinc/doc/source/nightly/" \
#     /disks/philmor/a/users/quarl/bin/export-tarball

# CHECKOUT='svn export http://svn.quarl.org/repos/coursesurvey/trunk/coursesurvey' \
#     DIR=coursesurvey                                                         \
#     FILENAME_TGZ="coursesurvey-rSVNREVISION.tar.gz"                          \
#     FILENAME_ZIP="coursesurvey-rSVNREVISION.zip"                             \
#     DESTINATION="ftp://upload.sourceforge.net/incoming"                      \
#     /home/quarl/bin/export-tarball

# Note: requires GNU tar (if you want to use other tar need to separate gzip
# step)

die()
{
    echo "ERROR in $0 on `hostname`:"
    echo "$1"
    [ "$2" ] && cat "$2"
    exit 1
}

reqeval()
{
    test -n "$VERBOSE" && echo "Executing: $1"
    eval "$1" || die "error executing: $1"
}

reqeval_log()
{
    test -n "$VERBOSE" && echo "Executing: $1"
    eval "$1" > $2 2>&1 || die "error executing: $1" $2
}

upload()
{
    if echo "$DESTINATION" | grep '^ftp://' >/dev/null ; then
        ftp_upload "$1"
    else
        file_upload "$1"
    fi
}

file_upload()
{
    test -n "$VERBOSE" && echo "Putting $1 in $DESTINATION"
    reqeval "mv $1 $DESTINATION"
}

ftp_upload()
{
    SERVER=`echo "$DESTINATION" | sed 's,^ftp://,,' | sed 's,/.*,,'`
    DIRECTORY=`echo "$DESTINATION" | sed 's,^ftp://[^/]*',,`

    test -n "$VERBOSE" && echo "Uploading $1 to ftp site $SERVER $DIRECTORY"
    reqeval "ncftpput $SERVER $DIRECTORY $1"
}

if [ -z "$USER" ]; then
    USER=$LOGNAME
fi

if ttyo 2>/dev/null ; then
    VERBOSE=1
fi

test -n "$CHECKOUT" || die "No CHECKOUT specified"
test -n "$DIR" || die "No DIR specified"
test -n "$FILENAME_TGZ" -o -n "$FILENAME_ZIP" || die "No FILENAME_TGZ nor FILENAME_ZIP specified"
test -n "$DESTINATION" || die "No DESTINATION specified"

TMPDIR=/tmp/export-tarball-$$

reqeval "rm -rf $TMPDIR"
reqeval "mkdir -p $TMPDIR"
reqeval "cd $TMPDIR"
reqeval_log "$CHECKOUT"      checkout.log

test -d "$DIR" || die "No DIR $DIR after CHECKOUT"

TODAY=`date +%Y-%m-%d`
# SVNREVISION=`svnlastchangedversion "$DIR" 2>/dev/null`

FILENAME_TGZ=`echo "$FILENAME_TGZ" | sed "s/TODAY/$TODAY/"`
FILENAME_ZIP=`echo "$FILENAME_ZIP" | sed "s/TODAY/$TODAY/"`

# FILENAME_TGZ=`echo "$FILENAME_TGZ" | sed "s/SVNREVISION/$SVNREVISION/"`
# FILENAME_ZIP=`echo "$FILENAME_ZIP" | sed "s/SVNREVISION/$SVNREVISION/"`

test -n "$FILENAME_TGZ" && reqeval_log "tar czvf $FILENAME_TGZ $DIR" tar.log
test -n "$FILENAME_ZIP" && reqeval_log "zip -r9 $FILENAME_ZIP $DIR" zip.log
upload $FILENAME_TGZ
upload $FILENAME_ZIP

reqeval "rm -rf $TMPDIR"
